﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SAC.Elink;
using SAC.sqlHelper;
using Newtonsoft.Json;
public partial class main3 : System.Web.UI.Page
{
    public double gl1 = 0;
    public double gl2 = 0;
    public double gl0 = 0;

    public double glzl1 = 0;
    public double glzl2 = 0;
    public double glzl0 = 0;

    public double wg1 = 0;
    public double wg2 = 0;
    public double wg0 = 0;

    public double wgzl1 = 0;
    public double wgzl2 = 0;
    public double wgzl0 = 0;

    public double ml1 = 0;
    public double ml2 = 0;
    public double ml0 = 0;

    public double srb1 = 0;
    public double srb2 = 0;
    public double srb0 = 0;

    public double gr1 = 0;
    public double gr2 = 0;
    public double gr0 = 0;

    public double zq1 = 0;
    public double zq2 = 0;
    public double zq0 = 0;

    public double zqyl1 = 0;
    public double zqyl2 = 0;
    public double zqyl0 = 0;

    public double zqwd1 = 0;
    public double zqwd2 = 0;
    public double zqwd0 = 0;

    public double zrwd1 = 0;
    public double zrwd2 = 0;
    public double zrwd0 = 0;

    public double zk1 = 0;
    public double zk2 = 0;
    public double zk0 = 0;

    public double lv1 = 0;
    public double lv2 = 0;
    public double lv0 = 0;

    public double rlv1 = 0;
    public double rlv2 = 0;
    public double rlv0 = 0;

    public double agcImg1 = 0;
    public double agcImg2 = 0;
    public double agcImg0 = 0;


    public double src1 = 0;
    public double src2 = 0;

    public string dtNow = "";

    public double fdl1 = 0;
    public double fdl2 = 0;
    public double fdl0 = 0;

    public double sw1 = 0;
    public double sw2 = 0;
    public double sw0 = 0;

    public double rml1 = 0;
    public double rml2 = 0;
    public double rml0 = 0;

    public double ryl1 = 0;
    public double ryl2 = 0;
    public double ryl0 = 0;

    public double tfdl1 = 0;
    public double tfdl2 = 0;
    public double tfdl0 = 0;

    public double tsw1 = 0;
    public double tsw2 = 0;
    public double tsw0 = 0;

    public double yfdl1 = 0;
    public double yfdl2 = 0;
    public double yfdl0 = 0;

    public double ysw1 = 0;
    public double ysw2 = 0;
    public double ysw0 = 0;

    public double yrml1 = 0;
    public double yrml2 = 0;
    public double yrml0 = 0;

    public double nsw1 = 0;
    public double nsw2 = 0;
    public double nsw0 = 0;

    public double nrml1 = 0;
    public double nrml2 = 0;
    public double nrml0 = 0;

    public double nfdl1 = 0;
    public double nfdl2 = 0;
    public double nfdl0 = 0;

    public string ywc1;
    public string ywc2;
    public string ywc0;

    public string nwc1;
    public string nwc2;
    public string nwc0;

    public string ydjh;
    public string ndjh;

    public string aqts;
    public string dtss;

    public string ycw1;
    public string ycw2;
    public string ycw0;

    public string ncw1;
    public string ncw2;
    public string ncw0;

    public double jhy = 0;
    public double jhn = 0;
    public string jhyvalue = "";
    public string jhnvalue = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Elink ek = new Elink();

        string param = Request["param"];
        if (param != null)
        {
            if (param == "query")
            {
                QueryTableList();
            }
        }
        //QueryTableList();
        #region 运行信息

        //////////////////////////

        DateTime dt = DateTime.Now.AddDays(-1);

        dtNow = dt.AddDays(+1).ToString("yyyy-MM-dd");

        dtss = DateTime.Now.ToString("yyyy-MM-dd");

        object obj;
        string bm = "";
        string sql = "";
        string qsrq = "";
        string jsrq = "";
        string errMsg = "";

        string pID1 = "fdj1zxyg";               //1号机组电量编号
        string pID2 = "fdj2zxyg";               //2号机组电量编号

        string xl1ID1 = "zb1zxyg";              //天瑞2836线主表正向有功 xl1zxyg              
        string xl2ID1 = "zb2zxyg";              //天瑞2836线主表正向有功 xl1zxyg 

        //日值
        qsrq = dt.ToString("yyyy-MM-dd 0:00:00");
        jsrq = dt.AddDays(+1).ToString("yyyy-MM-dd 00:00:00");

        SAC.Power.PowerCale pc = new SAC.Power.PowerCale();

        fdl1 = double.Parse(pc.GetPower(fdl1, pID1, qsrq, jsrq, bm).ToString()) / 10000;
        fdl2 = double.Parse(pc.GetPower(fdl2, pID2, qsrq, jsrq, bm).ToString()) / 10000;

        fdl0 = fdl2 + fdl1;

        sw1 = double.Parse(pc.GetPower(sw1, xl1ID1, qsrq, jsrq, bm).ToString()) / 10000;
        sw2 = double.Parse(pc.GetPower(sw2, xl2ID1, qsrq, jsrq, bm).ToString()) / 10000;

        sw0 = sw1 + sw2;

        //燃煤量
        sql = "select sum(煤量1) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            rml1 = double.Parse(obj.ToString());

        sql = "select sum(煤量2) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            rml2 = double.Parse(obj.ToString());

        sql = "select sum(煤量1)+sum(煤量2) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            rml0 = double.Parse(obj.ToString());

        //燃油量
        sql = "SELECT sum(用油量) FROM 用油量数据表  where 日期 BETWEEN '" + DateTime.Parse(qsrq).ToString("yyyy-01-01 0:00:00") + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "' and 机组=1";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            ryl1 = double.Parse(obj.ToString());

        sql = "select sum(用油量) from 用油量数据表  where 日期 BETWEEN '" + DateTime.Parse(qsrq).ToString("yyyy-01-01 0:00:00") + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "' and 机组=2";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            ryl2 = double.Parse(obj.ToString());

        sql = "select sum(用油量) from 用油量数据表  where 日期 BETWEEN '" + DateTime.Parse(qsrq).ToString("yyyy-01-01 0:00:00") + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            ryl0 = double.Parse(obj.ToString());

        //月累计

        qsrq = dt.ToString("yyyy-MM-01 0:00:00");
        jsrq = dt.AddDays(+1).ToString("yyyy-MM-dd 00:00:00");

        yfdl1 = double.Parse(pc.GetPower(yfdl1, pID1, qsrq, jsrq, bm).ToString()) / 10000;
        yfdl2 = double.Parse(pc.GetPower(yfdl2, pID2, qsrq, jsrq, bm).ToString()) / 10000;

        yfdl0 = yfdl2 + yfdl1;

        ysw1 = double.Parse(pc.GetPower(ysw1, xl1ID1, qsrq, jsrq, bm).ToString()) / 10000;
        ysw2 = double.Parse(pc.GetPower(ysw2, xl2ID1, qsrq, jsrq, bm).ToString()) / 10000;

        ysw0 = ysw1 + ysw2;

        //燃煤量
        sql = "select sum(煤量1) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            yrml1 = double.Parse(obj.ToString());

        sql = "select sum(煤量2) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            yrml2 = double.Parse(obj.ToString());

        sql = "select sum(煤量1)+sum(煤量2) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            yrml0 = double.Parse(obj.ToString());


        double daysM = DateTime.DaysInMonth(dt.Year, dt.Month);
        double dayM = dt.Day;

        //月度计划值
        sql = "SELECT 计划值 FROM 月度电量计划表 where 日期='" + qsrq + "' and 机组=1";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "0")
        {
            if (obj != null && obj.ToString() != "")
            {
                double wc1 = yfdl1 / double.Parse(obj.ToString()) * 100;
                ywc1 = wc1.ToString("f2");
                double cw1 = wc1 - dayM / daysM * 100;

                if (cw1 < 0)
                    ycw1 = "<font color=\"#FF0000\">" + cw1.ToString("f2") + "</font> ";
                else
                    ycw1 = cw1.ToString("f2");
            }
            else
            {
                ywc1 = "&nbsp;";
                ycw1 = "&nbsp;";
            }
        }
        else
        {
            ywc1 = "/";
            ycw1 = "/";
        }
        sql = "SELECT 计划值 FROM 月度电量计划表 where 日期='" + qsrq + "' and 机组=2";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "0")
        {
            if (obj != null && obj.ToString() != "")
            {
                double wc2 = yfdl2 / double.Parse(obj.ToString()) * 100;
                ywc2 = wc2.ToString("f2");
                double cw2 = wc2 - dayM / daysM * 100;


                if (cw2 < 0)
                    ycw2 = "<font color=\"#FF0000\">" + cw2.ToString("f2") + "</font> ";
                else
                    ycw2 = cw2.ToString("f2");


            }
            else
            {
                ywc2 = "&nbsp;";
                ycw2 = "&nbsp;";
            }
        }
        else
        {
            ywc2 = "/";
            ycw2 = "/";
        }
        sql = "SELECT 计划值 FROM 月度电量计划表 where 日期='" + qsrq + "' and 机组=0";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "0")
        {
            if (obj != null && obj.ToString() != "")
            {
                ydjh = obj.ToString();
                double wc3 = yfdl0 / double.Parse(obj.ToString()) * 100;
                ywc0 = wc3.ToString("f2");
                double cw3 = wc3 - dayM / daysM * 100;

                if (cw3 < 0)
                    ycw0 = "<font color=\"#FF0000\">" + cw3.ToString("f2") + "</font> ";
                else
                    ycw0 = cw3.ToString("f2");

            }
            else
            {
                ydjh = "&nbsp;";
                ywc0 = "&nbsp;";
                ycw0 = "&nbsp;";
            }
        }
        else
        {
            ywc0 = "/";
            ycw0 = "/";
        }


        //同期
        qsrq = dt.AddYears(-1).ToString("yyyy-MM-01 0:00:00");
        jsrq = dt.AddYears(-1).AddDays(+1).ToString("yyyy-MM-dd 00:00:00");

        tfdl1 = double.Parse(pc.GetPower(tfdl1, pID1, qsrq, jsrq, bm).ToString()) / 10000;
        tfdl2 = double.Parse(pc.GetPower(tfdl2, pID2, qsrq, jsrq, bm).ToString()) / 10000;

        tfdl0 = tfdl2 + tfdl1;

        tsw1 = double.Parse(pc.GetPower(tsw1, xl1ID1, qsrq, jsrq, bm).ToString()) / 10000;
        tsw2 = double.Parse(pc.GetPower(tsw2, xl2ID1, qsrq, jsrq, bm).ToString()) / 10000;

        tsw0 = tsw1 + tsw2;

        //年累计
        qsrq = dt.ToString("yyyy-01-01 0:00:00");
        jsrq = dt.AddDays(+1).ToString("yyyy-MM-dd 00:00:00");

        nfdl1 = double.Parse(pc.GetPower(nfdl1, pID1, qsrq, jsrq, bm).ToString()) / 10000;
        nfdl2 = double.Parse(pc.GetPower(nfdl2, pID2, qsrq, jsrq, bm).ToString()) / 10000;

        nfdl0 = nfdl2 + nfdl1;

        nsw1 = double.Parse(pc.GetPower(nsw1, xl1ID1, qsrq, jsrq, bm).ToString()) / 10000;
        nsw2 = double.Parse(pc.GetPower(nsw2, xl2ID1, qsrq, jsrq, bm).ToString()) / 10000;

        nsw0 = nsw1 + nsw2;

        //燃煤量
        sql = "select sum(煤量1) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            nrml1 = double.Parse(obj.ToString());

        sql = "select sum(煤量2) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            nrml2 = double.Parse(obj.ToString());

        sql = "select sum(煤量1)+sum(煤量2) from 入炉煤质分析表  where 日期 BETWEEN '" + qsrq + "' AND '" + DateTime.Parse(jsrq).AddDays(-1).ToString("yyyy-MM-dd 23:59:59") + "'";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
            nrml0 = double.Parse(obj.ToString());

        double daysY = this.retDaysInYear(dt);
        double dayY = this.retDayYear(dt);

        //年度计划值
        sql = "SELECT 计划值 FROM 年度电量计划表 where 日期='" + qsrq + "' and 机组=1";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "0")
        {
            if (obj != null && obj.ToString() != "")
            {
                double wc1 = nfdl1 / double.Parse(obj.ToString()) * 100;
                nwc1 = wc1.ToString("f2");

                double cw1 = wc1 - dayY / daysY * 100;

                if (cw1 < 0)
                    ncw1 = "<font color=\"#FF0000\">" + cw1.ToString("f2") + "</font> ";
                else
                    ncw1 = cw1.ToString("f2");
            }
            else
            {
                ncw1 = "&nbsp;";
                nwc1 = "&nbsp;";
            }
        }
        else
        {

            ncw1 = "/";
            nwc1 = "/";
        }
        sql = "SELECT 计划值 FROM 年度电量计划表 where 日期='" + qsrq + "' and 机组=2";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "0")
        {
            if (obj != null && obj.ToString() != "")
            {
                double wc2 = nfdl2 / double.Parse(obj.ToString()) * 100;
                nwc2 = wc2.ToString("f2");

                double cw2 = wc2 - dayY / daysY * 100;

                if (cw2 < 0)
                    ncw2 = "<font color=\"#FF0000\">" + cw2.ToString("f2") + "</font> ";
                else
                    ncw2 = cw2.ToString("f2");

            }
            else
            {
                nwc2 = "&nbsp;";
                ncw2 = "&nbsp;";
            }
        }
        else
        {
            nwc2 = "/";
            ncw2 = "/";

        }
        sql = "SELECT 计划值 FROM 年度电量计划表 where 日期='" + qsrq + "' and 机组=0";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "0")
        {
            if (obj != null && obj.ToString() != "")
            {
                ndjh = obj.ToString();
                double wc3 = nfdl0 / double.Parse(obj.ToString()) * 100;
                nwc0 = wc3.ToString("f2");

                double cw3 = wc3 - dayY / daysY * 100;

                if (cw3 < 0)
                    ncw0 = "<font color=\"#FF0000\">" + cw3.ToString("f2") + "</font> ";
                else
                    ncw0 = cw3.ToString("f2");
            }
            else
            {
                ndjh = "&nbsp;";
                nwc0 = "&nbsp;";
                ncw0 = "&nbsp;";
            }
        }
        else
        {
            nwc0 = "/";
            ncw0 = "/";
        }


        //安全天数
        sql = " SELECT 开始日期 FROM 安全天数事件表";

        obj = DBsql.RunSingle(sql, out errMsg);

        if (obj != null && obj.ToString() != "")
        {
            TimeSpan ts = DateTime.Now.AddDays(-1) - DateTime.Parse(obj.ToString());
            aqts = ts.Days.ToString();
        }
        else
            aqts = "&nbsp;";

        #endregion

        double daysMonth = this.retDayMonthS(dt);
        double daysYear = this.retDayYearS(dt);

        //月度
        try
        {
            jhy = (double.Parse(ydjh) - yfdl0) / daysMonth;
            jhn = (double.Parse(ndjh) - nfdl0) / daysYear;

            if (jhy.ToString() == "正无穷大")
            { jhyvalue = "-"; }
            else { jhyvalue = jhy.ToString("f0"); }

            if (jhn.ToString() == "正无穷大")
            { jhnvalue="-";}else
            {jhnvalue=jhn.ToString("f0");}
        }
        catch
        { jhy = 0; jhn = 0; }

        //try
        //{
        //    double v = double.Parse(jhy.ToString());
        //    jhyvalue = v.ToString("f0");
        //}
        //catch { jhyvalue = "-"; };
    }


    private double retDaysInYear(DateTime dt)
    {
        DateTime qsrq;
        DateTime jsrq;

        TimeSpan ts;

        qsrq = DateTime.Parse(dt.ToString("yyyy-01-01 0:00:00"));
        jsrq = DateTime.Parse(dt.AddYears(+1).ToString("yyyy-01-01 0:00:00"));

        ts = jsrq - qsrq;

        if (ts.Days > 0)
        {
            return ts.Days;
        }
        else { return 0; }
    }

    private double retDayYear(DateTime dt)
    {
        DateTime qsrq;
        DateTime jsrq;

        TimeSpan ts;

        qsrq = DateTime.Parse(dt.ToString("yyyy-01-01 0:00:00"));
        jsrq = DateTime.Parse(dt.ToString("yyyy-MM-dd 0:00:00"));

        ts = jsrq - qsrq;

        if (ts.Days > 0)
        {
            return ts.Days + 1;
        }
        else { return 0; }
    }

    //一个月还是剩多少天
    private double retDayMonthS(DateTime dt)
    {
        SAC.Helper.DateHelper dh = new SAC.Helper.DateHelper();

        DateTime qsrq;
        DateTime jsrq;

        TimeSpan ts;

        qsrq = DateTime.Parse(dt.ToString("yyyy-MM-dd 0:00:00"));
        jsrq = dh.GetLastDayOfMonth(dt);

        ts = jsrq - qsrq;

        if (ts.Days > 0)
        {
            return ts.Days;
        }
        else { return 0; }
    }
    //一年还是剩多少天
    private double retDayYearS(DateTime dt)
    {
        SAC.Helper.DateHelper dh = new SAC.Helper.DateHelper();

        DateTime qsrq;
        DateTime jsrq;

        TimeSpan ts;

        qsrq = DateTime.Parse(dt.ToString("yyyy-MM-dd 0:00:00"));
        jsrq = DateTime.Parse(dt.ToString("yyyy-12-31 0:00:00"));

        ts = jsrq - qsrq;

        if (ts.Days > 0)
        {
            return ts.Days;
        }
        else { return 0; }
    }


    protected void btnRec1_Click(object sender, EventArgs e)
    {
        Response.Redirect("../xtt/ednaview.asp?File=WHSIS.APP:ALL\\WGT.rtv");
    }

    protected void QueryTableList()
    {
        int ret = 0;
        double valb = 0;
        double vale = 0;
        double val1 = 0;
        double val2 = 0;
        double val0 = 0;

        string qsrq = DateTime.Now.ToString("yyyy-MM-dd 0:00:00");

        ArrayList list = new ArrayList();

        Elink ek = new Elink();

        string dl1 = "WHSIS.KHPSH.CI000205";
        string dl2 = "WHSIS.KHPSH.CI000209";

        ek.GetHisValue(dl1, qsrq, ref ret, ref valb);
        vale = ek.GetRTValue(dl1);

        val1 = (vale - valb) * 1100000 / 10000;

        valb = 0;
        vale = 0;

        ek.GetHisValue(dl2, qsrq, ref ret, ref valb);
        vale = ek.GetRTValue(dl2);

        val2 = (vale - valb) * 1100000 / 10000;

        val0 = val1 + val2;

        string strApp = val1.ToString("f2") + "," + val2.ToString("f2") + "," + val0.ToString("f2");

        //int[] randomArray = new int[20];
        //Random random = new Random();
        //randomArray[0] = random.Next(1, 10);
        //string strApp = "";

        //for (int i = 1; i < 4; i++)
        //{
        //    strApp += random.Next(1, 10) + ",";
        //}
        //strApp = strApp.TrimEnd(',');
        object obj = new { tableInfo = strApp };
        string result = JavaScriptConvert.SerializeObject(obj);
        Response.Write(result);
        Response.End();
    }
}
